﻿using ArticleInputDto = Corporation.Project.Dtos.ArticleInputDto;
using Entity = Corporation.Project.Entities.Article;
using OutputDto = Corporation.Project.Dtos.ArticleOutputDto;

namespace Corporation.Project.Api.Areas.Admin.Controllers
{
    /// <summary>
    /// 管理-文章信息
    /// </summary>
    [Description("管理-文章信息")]
    [ModuleInfo(Code = "Article", Name = "文章信息", OrderNo = 99, Position = "Project", PositionName = "Project模块")]
    public class ArticleController: AdminControllerBase
    {
        /// <summary>
        /// 获取 设备服务
        /// </summary>
        protected ArticleStore ArticleStore => ServiceProvider.GetService<ArticleStore>();

        /// <summary>
        /// 读取文章列表
        /// </summary>
        /// <param name="request">分布查询请求</param>
        /// <returns>文章列表</returns>
        [ModuleInfo]
        [HttpPost]
        [Description("读取")]
        public PageData<OutputDto> Read(PageRequest request)
        {
            Check.NotNull(request, nameof(request));
            IFunction function = this.GetExecuteFunction();

            Expression<Func<Entity, bool>> predicate = FilterService.GetExpression<Entity>(request.FilterGroup);
            var page = CacheService.ToPageCache<Entity, OutputDto>(ArticleStore.Entities, predicate, request.PageCondition, function);

            return page.ToPageData();
        }

        /// <summary>
        /// 新增文章实体
        /// </summary>
        /// <param name="dtos">新增文章信息</param>
        /// <returns>JSON操作结果</returns>
        [Description("新增")]
        [DependOnFunction(nameof(Read))]
        [ModuleInfo]
        [UnitOfWork]
        [HttpPost]
        public async Task<AjaxResult> Create(ArticleInputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));
            List<string> ids = new();
            List<OutputDto> outputDtos = new();
            foreach (var dto in dtos)
            {
                var result = await ArticleStore.InsertByDtoAsync(dto);
                if (!result.Succeeded)
                {
                    return result.ToAjaxResult();
                }
                ids.Add(dto.Id.ToString());
                outputDtos.Add(dto.MapTo<OutputDto>());
            }
            return AjaxResult.Success(outputDtos, string.Format("实体“{0}”创建成功", ids.ExpandAndToString()));
        }

        /// <summary>
        /// 更新文章实体
        /// </summary>
        /// <param name="dtos">Demo信息</param>
        /// <returns>JSON操作结果</returns>
        [HttpPost]
        [ModuleInfo]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("更新")]
        public async Task<AjaxResult> Update(ArticleInputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));
            List<string> ids = new();
            List<OutputDto> outputDtos = new();
            foreach (var dto in dtos)
            {
                var result = await ArticleStore.UpdateByDtoAsync(dto);
                if (!result.Succeeded)
                {
                    return result.ToAjaxResult();
                }
                ids.Add(dto.Id.ToString());
                outputDtos.Add(dto.MapTo<ArticleInputDto, ArticleOutputDto>());
            }
            return AjaxResult.Success(outputDtos, I18N.T("实体“{0}”更新成功", ids.ExpandAndToString()));
        }

        /// <summary>
        /// 删除文章实体
        /// </summary>
        /// <param name="ids">要删除的编号</param>
        /// <returns>JSON操作结果</returns>
        [HttpPost]
        [ModuleInfo]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("删除")]
        public async Task<AjaxResult> Delete(Guid[] ids)
        {
            Check.NotNull(ids, nameof(ids));
            var result = await ArticleStore.DeleteAsync(ids);
            return result.ToAjaxResult();
        }

    }
}
